﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CNVP.Framework.Utils;
using System.Text.RegularExpressions;
using CNVP.Config;

namespace CNVP.JShop.Web.Member
{
    public partial class Register : System.Web.UI.Page
    {
        BLL.CNVP_Users bll = new BLL.CNVP_Users();
        Model.CNVP_Users model = new Model.CNVP_Users();
        public int seoInId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region js验证
                txtUserName.Attributes.Add("onFocus", "RegTipInfo('lbUserName','请输入会员账户．')");
                txtUserName.Attributes.Add("onBlur", "return RegCheck('txtUserName','lbUserName')");
                txtPwd1.Attributes.Add("onFocus", "RegTipInfo('lbPwd1','请输入密码．')");
                txtPwd1.Attributes.Add("onBlur", "return RegCheck('txtPwd1','lbPwd1')");
                txtPwd2.Attributes.Add("onFocus", "RegTipInfo('lbPwd2','请输入确认密码，与上面一致．')");
                txtPwd2.Attributes.Add("onBlur", "return RegCheck('txtPwd2','lbPwd2')");
                txtEmail.Attributes.Add("onFocus", "RegTipInfo('lbEmail','请输入Email．')");
                txtEmail.Attributes.Add("onBlur", "return RegCheck('txtEmail','lbEmail')");
                btUp.Attributes.Add("onClick", "return CheckAll()");
                #endregion
            }
            
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            string UserName = txtUserName.Text.Trim();
            string UserPass1 = txtPwd1.Text.Trim();
            string UserPass2 = txtPwd2.Text.Trim();
            string Email = txtEmail.Text.Trim();
            string _UserFilter = UIConfig.UserFilter;
            if (string.IsNullOrEmpty(UserName) || UserName.Length < 2 || UserName.Length > 20)
            {
                MessageUtils.ShowPre("会员账户不能为空或格式不正确！");
                return;
            }
            if (_UserFilter != "")
            {
                string[] UserFilterArray = _UserFilter.Split(Convert.ToChar("|"));
                for (int i = 0; i < UserFilterArray.Length; i++)
                {
                    if (UserFilterArray[i].ToLower() == UserName.ToLower())
                    {
                        MessageUtils.ShowPre("会员账户不允许使用" + _UserFilter + "！");
                        return;
                    }
                }
            }
            if (String.IsNullOrEmpty(UserPass1) || UserPass1.Length < 6 || UserPass1.Length > 20 )
            {
                MessageUtils.ShowPre("新密码不能为空或格式不正确！");
                return;
            }
            if (String.IsNullOrEmpty(UserPass2) || UserPass2.Length < 6 || UserPass2.Length > 20 )
            {
                MessageUtils.ShowPre("确认密码不能为空或格式不正确！");
                return;
            }
            if (UserPass2 != UserPass1)
            {
                MessageUtils.ShowPre("新密码和确认密码不相等！");
                return;
            }
            if (String.IsNullOrEmpty(Email) || !Public.IsEmail(Email))
            {
                MessageUtils.ShowPre("邮箱不能为空或格式不正确！");
                return;
            }
            IList<Model.CNVP_Users> users_list = bll.QurryListByUserName(UserName);
            if (users_list.Count > 0)
            {
                MessageUtils.ShowPre("用户已经存在，请您换个账户！");
            }
            else
            {
                int _ID=0;
                var model_UserLevel=new BLL.CNVP_UserLevel().LoadBysqlWhere("where IsAcquiesce=1");
                if (model_UserLevel != null)
                {
                    _ID = model_UserLevel.ID;
                }
                int rows = new BLL.CNVP_Users().Add_CNVP_Users(new Model.CNVP_Users()
                {
                    UserName = UserName,
                    UserPass = EncryptUtils.MD5(UserPass1),
                    Email = Email,
                    PointsNum = 0,
                    TotalAmount = 0,
                    AddTime = DateTime.Now.AddSeconds(1),
                    AddIP = HttpContext.Current.Request.UserHostAddress,
                    EndLoginTime = DateTime.Now.AddSeconds(1),
                    EndLoginIP = HttpContext.Current.Request.UserHostAddress,
                    OtherText = "",
                    IsDelete = 0,
                    LoginNum=0,
                    UserLevelID = _ID

                });
                if (rows == 1)
                {
                    if (!String.IsNullOrEmpty(Email) && Public.IsEmail(Email))
                    {
                        MailUtils _MailUtils = new MailUtils();
                        _MailUtils.SmtpServer = UIConfig.MailServer;
                        _MailUtils.UserName = UIConfig.MailUserName;
                        _MailUtils.UserPass = UIConfig.MailUserPass;

                        string toSubject = UIConfig.SiteName + "欢迎您的加入。";
                        string toBody = "您的用户名：" + UserName + " <br/> 密码：" + UserPass1;

                        bool flag = _MailUtils.Send(Email, UIConfig.MailInfo, toSubject, toBody, true);
                    }
                    UserLogin(UserName, UserPass1);
                }
                else
                {
                    MessageUtils.ShowPre("添加失败！");
                }
            }
        }

        private void UserLogin(string userName, string userpwd)
        {
            BLL.CNVP_Users bll = new BLL.CNVP_Users();
            Model.CNVP_Users model = new Model.CNVP_Users();
            model.UserName = userName;
            model.UserPass = EncryptUtils.MD5(userpwd);
            model.IsDelete = 0;
            model = bll.cust_LoginByName(model);
            if (model != null)
            {
                Response.Cookies["" + UIConfig.CookiesName + "UserID"].Value = EncryptUtils.EncodeCookies(model.ID.ToString());
                Response.Cookies["" + UIConfig.CookiesName + "UserName"].Value = EncryptUtils.EncodeCookies(model.UserName);
                Response.Cookies["" + UIConfig.CookiesName + "UserLevelID"].Value = EncryptUtils.EncodeCookies(model.UserLevelID.ToString());
                //更新
                //更新
                model.EndLoginIP = HttpContext.Current.Request.UserHostAddress;
                model.EndLoginTime = DateTime.Now.AddMinutes(1);
                bll.cust_UpdateLogin(model);
                MessageUtils.ShowRedirect("恭喜您注册成为" + UIConfig.SiteName + "会员！", "/member/Member.aspx");
            }
        }
    }
}